package elicecn.web.struts2.model;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import elicecn.web.struts2.dao.IBatisDaoUtils;

public abstract class BaseModelImpl implements BaseModel {
	private static final long serialVersionUID = 8451433847526733217L;
	public static final int FLAG_NO = 0;
	public static final int FLAG_YES = 1;

	public static final int IS_DELETE_NO = 0;
	public static final int IS_DELETE_YES = 1;

	@SuppressWarnings("unchecked")
	public String toString() {
		StringBuffer propBuffer = new StringBuffer();
		Field[] fields = this.getClass().getDeclaredFields();
		propBuffer.append("[").append(this.getClass()).append("]");

		for (Field field : fields) {
			String fieldName = field.getName();
			Object fieldValue = null;
			String getterMethod = "get"
					+ Character.toUpperCase(fieldName.charAt(0))
					+ fieldName.substring(1);
			try {
				Method getMethod = this.getClass().getMethod(getterMethod,
						(Class[]) null);
				Object o = getMethod.invoke(this, (Object[]) null);
				if (o instanceof Map || o instanceof List) {
					continue;
				}

				if (o instanceof BaseModel) {
					fieldName += "Id";
					fieldValue = IBatisDaoUtils
							.getPrimaryKeyValue((BaseModel) o);
				} else {
					fieldValue = o;
				}

				propBuffer.append(fieldName).append(":").append(fieldValue)
						.append(",");

			} catch (Exception e) {
			}

		}

		return propBuffer.substring(0, propBuffer.length() - 1);
	}
}
